package net.neptech.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.neptech.mapper.NotificationMessageMapper;
import net.neptech.model.dto.MessageDto;
import net.neptech.model.dto.PageMessageDto;
import net.neptech.model.workflow.NotificationMessage;
import net.neptech.service.MessageService;
import net.neptech.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Saya
 * @Date 2025/8/22 10:10
 * @Description TODO
 **/
@Service
//这个BaseMapper写法有问题，待修补 TODO
public class MessageServiceImpl extends ServiceImpl<NotificationMessageMapper, NotificationMessage> implements MessageService {
    /**
     * 查询未读数量
     */
    @Override
    public Dict queryUnreadNum(Long lastId) {
        if (lastId == null) {
            lastId = 0L;
        }
        String userId = SecurityUtils.getUserId().toString();
        Long num = this.lambdaQuery()
                .eq(NotificationMessage::getHasRead, false)
                .eq(NotificationMessage::getUserId, userId).count();

        NotificationMessage notificationMessage = this.lambdaQuery()
                .gt(NotificationMessage::getMessageId, lastId)
                .eq(NotificationMessage::getUserId, userId).orderByDesc(NotificationMessage::getCreateTime)
                .last("limit 1").one();

        return Dict.create().set("num", num)
                .set("maxId", notificationMessage == null ? null : notificationMessage.getMessageId())
                .set("title", notificationMessage == null ? null : notificationMessage.getTitle())
                .set("content", notificationMessage == null ? null : notificationMessage.getContent());
    }

    /**
     * 保存消息至数据库
     */
    @Override
    public void saveMessage(MessageDto messageDto) {
        NotificationMessage notificationMessage = BeanUtil.copyProperties(messageDto, NotificationMessage.class);
        save(notificationMessage);
    }

    /**
     * 查询列表
     */
    @Override
    public Page<NotificationMessage> queryList(PageMessageDto pageDto) {
        return lambdaQuery()
                .eq(NotificationMessage::getUserId, SecurityUtils.getUserId())
                .eq(pageDto.getHasRead() != null, NotificationMessage::getHasRead, pageDto.getHasRead())
                .orderByDesc(NotificationMessage::getCreateTime)
                .page(new Page<>(pageDto.getPageNum(), pageDto.getPageSize()));
    }

    /**
     * 删除消息
     */
    @Override
    public void delete(PageMessageDto messageDto) {
        removeById(messageDto.getId());
    }

    /**
     * 置为已读
     */
    @Override
    public void read(PageMessageDto messageDto) {
        Long userId = SecurityUtils.getUserId();
        this.lambdaUpdate()
                .set(NotificationMessage::getHasRead, true)
                .eq(NotificationMessage::getUserId, userId)
                .eq(NotificationMessage::getMessageId, messageDto.getId())
                .eq(NotificationMessage::getHasRead, false)
                .update(new NotificationMessage());
    }

    /**
     * 全部已读
     */
    @Override
    public void readAll() {
        Long userId = SecurityUtils.getUserId();

        this.lambdaUpdate().set(NotificationMessage::getHasRead, true)
                .eq(NotificationMessage::getUserId, userId)
                .eq(NotificationMessage::getHasRead, false)
                .update(new NotificationMessage());
    }
    /**
     * 清理数据
     */
    @Override
    public void clearProcess(List<String> flowIdList) {
        this.lambdaUpdate()
                .in(NotificationMessage::getFlowId, flowIdList)
                .remove();
    }
}
